<template>
  <div class="container">
    <!--标题-->
    <div style="margin-top: 200px">
      <div style="font-size: 25px;font-weight: bold">登录</div>
      <div style="font-size: 14px;color:grey">输入用户名和密码进行登录</div>
    </div>
    <!--表单-->
    <div style="margin-top: 50px;">
      <el-form ref="loginForm" 
        :model="loginForm" 
        :rules="loginRules"
         class="login-form"
          auto-complete="on"
        label-position="left">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" ref="username" v-model="loginForm.username" placeholder="Username"
            name="username" type="text" tabindex="1" auto-complete="on" style="width: 90%;" />
        </el-form-item>

        <el-form-item prop="password">
          <el-input show-password prefix-icon="el-icon-lock" :key="passwordType" ref="password"
            v-model="loginForm.password" :type="passwordType" placeholder="Password" name="password" tabindex="2"
            auto-complete="on" @keyup.enter.native="handleLogin" style="width: 90%;" />
        </el-form-item>
      </el-form>
      <!--记住我，忘记密码-->
      <div style="margin-top: 10px;margin-left: 20px;">
        <el-row>
          <el-col :span="12" style="text-align: left">
            <el-checkbox label="记住我" size="large" />
          </el-col>
          <el-col :span="12" style="text-align: right">
            <el-link @click="goToReset">忘记密码？</el-link>
          </el-col>
        </el-row>
      </div>
      <el-button :loading="loading" style="width: 270px;margin-top: 50px;" type="success" plain
        @click.native.prevent="handleLogin">登录</el-button>
      <el-divider>
        <span style="color: grey;font-size: 13px">没有账号</span>
      </el-divider>
      <div>
        <el-button @click="goToRegister" style="width: 270px" type="warning" plain>注册账号</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { login } from '@/api/user'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    goToReset() {
      this.$router.push('/resetPaw')
    },
    goToRegister() {
      this.$router.push('/register')
    },

    handleLogin() {
      //this.$router.push({ path:  '/welcome' })
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          // login(this.loginForm).then(() => {
          //   alert('登陆成功')
          //   this.$router.push({ path: this.redirect || '/welcome' })
          //   this.loading = false
          // }).catch(() => {
          //   this.loading = false
          //   alert('error')
          // })
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push('/welcome/index')
            //this.$router.push({ path: this.redirect || '/welcome' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */



</style>

<style lang="scss" scoped>


.container {
  display: block;
  text-align: center;
  height: 100%;
  background-color: rgb(252, 252, 241);
}

// .el-input__inner {
//   background-color: rgb(252, 252, 241);
// }

</style>
